package com.gitee.ywj1352.test.observer.core;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class EventListener implements IListener{

    //注册器

    protected Map<Enum,Event> events= new HashMap();


    @Override
    public  void addListenter(Enum eventType, Object target, Method callblack){
        //注册事件
        //用反射调用这个方法
        events.put(eventType,new Event(target,callblack));
    }

    @Override
    public void trigger(Event e){
        e.setSource(this);
        e.setTime(System.currentTimeMillis());
        try {
            //调用 callBlack 方法
            e.getCallback().invoke(e.getTarget(),e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

    }

    @Override
    public void trigger(Enum call){
        if (!this.events.containsKey(call)){
            return;
        }
        Event event = this.events.get(call);
        event.setTrigger(call.toString());
        trigger(event);
    }

}
